package solutions.ferpa.ztest.intf;

import br.com.telescope.adsrt.dom.DomId;
import br.com.telescope.adsrt.dom.DomOpcao;
import br.com.telescope.adsrt.dom.DomText;
import br.com.telescope.adsrt.dom.DomUpper;
import br.com.telescope.adsrt.lib.*;
import br.com.telescope.adsrt.lib.AdsException;
import br.com.telescope.adsrt.lib.Field;
import br.com.telescope.adsrt.lib.Form;
import br.com.telescope.sql.ModelQuery;
import br.com.telescope.t2.ServiceModel;
import br.com.telescope.t2.exception.ApplicationRuntimeException;
import br.com.telescope.t2.feature.DeleteFeature;
import br.com.telescope.t2.feature.ExportFeature;
import br.com.telescope.t2.feature.ImportFeature;
import br.com.telescope.t2.feature.InsertFeature;
import br.com.telescope.t2.feature.ListFeature;
import br.com.telescope.t2.feature.LookupFeature;
import br.com.telescope.t2.feature.QueryFeature;
import br.com.telescope.t2.feature.ViewFeature;
import br.com.telescope.util.ObjectUtil;
import java.util.ArrayList;
import java.util.List;
import solutions.ferpa.ztest.ent.*;

/**
 * Cadastro das operações de uma função.
 * <p>
 * Esta interface lista todas as operações que uma determinada função 
 * pode ter, permitindo o usuário excluir e incluir novas operações.
 * <P>
 * 
 * <P>
 * 
 * <P>
 * Telescope origin: RTSYSINF.CAD_FUNCAO_OPERACAO
 * <P>
 * Base entity: RTSYSINF.OPERACOES_FUNCOES
 * <P>
 * @author TELESCOPE - Build task T2 Web Interface Generator
 * <P>
 * @version 0.1
 */
public class CadFuncaoOperacao extends ServiceModel {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public static final String GLOBAL_NAME = "RTSYSINF.CAD_FUNCAO_OPERACAO";
    
    @Override
    public String globalName() {
        return GLOBAL_NAME;
    }
    
    {
        setTitle("Cadastro das operações de uma função");
    }
    
    protected EntOperacoesFuncoes opfu;
    protected EntFuncoesSistemas func;
    protected EntOperacoesSistemas opsi;
    protected EntInterfacesSistemas intf;
    protected EntOperacoesFuncoes rec; // NEED_TJW1_COMPATIBILITY
    
    // Service parameters
    private DomId pFuncaoId;
    
    // Record items
    private DomId aId;
    private DomId aFuncaoId;
    private DomOpcao aOperacaoId;
    private DomUpper aFuncaoSistema;
    private DomUpper aOperacao;
    private DomText aTitulo;
    private DomText aIntfTitulo;
    private DomOpcao aOperTitulo;
    
    // Query items
    private DomText qTitulo;
    private DomUpper qOperacao;
    
    @Override
    public String getBaseSql() {
        return "select OPFU.*"
                + " , FUNC.FUNCAO_SISTEMA as FUNCAO_SISTEMA"
                + " , OPSI.OPERACAO as OPERACAO"
                + " , OPSI.TITULO as TITULO"
                + " , INTF.TITULO as INTF_TITULO"
                + " , (opsi.operacao||' - '||opsi.titulo) as OPER_TITULO"
                + " , FUNC.ID as Z_FUNC_ID"
                + " , OPSI.ID as Z_OPSI_ID"
                + " , INTF.ID as Z_INTF_ID"
                + " from OPERACOES_FUNCOES OPFU"
                + " join FUNCOES_SISTEMAS FUNC on FUNC.ID = OPFU.FUNCAO_ID"
                + " join OPERACOES_SISTEMAS OPSI on OPSI.ID = OPFU.OPERACAO_ID"
                + " left join INTERFACES_SISTEMAS INTF on INTF.ID = OPSI.INTERFACE_ID";
    }
    
    @Override
    public void initAttributes() {
        
        aId = new DomId();
        aId.context(ctx).name("ID").label("Id");
        
        aFuncaoId = new DomId();
        aFuncaoId.context(ctx).name("FUNCAO_ID").label("Funcao");
        
        aOperacaoId = new DomOpcao();
        aOperacaoId.context(ctx).name("OPERACAO_ID").label("Operação").group(true);
        
        aFuncaoSistema = new DomUpper();
        aFuncaoSistema.context(ctx).name("FUNCAO_SISTEMA").label("Função");
        aFuncaoSistema.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT FUNCAO_SISTEMA as valor"
                + " , FUNCAO_SISTEMA as pesquisar"
                + " , FUNCAO_SISTEMA as descricao"
                + " from FUNCOES_SISTEMAS FUNC)", "q"));
        aFuncaoSistema.setAutocompleteLength(1);
        
        aOperacao = new DomUpper();
        aOperacao.context(ctx).name("OPERACAO").label("Operação");
        aOperacao.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT OPERACAO as valor"
                + " , OPERACAO as pesquisar"
                + " , OPERACAO as descricao"
                + " from OPERACOES_SISTEMAS OPSI)", "q"));
        aOperacao.setAutocompleteLength(2);
        
        aTitulo = new DomText();
        aTitulo.context(ctx).name("TITULO").label("Feature");
        aTitulo.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT TITULO as valor"
                + " , TITULO as pesquisar"
                + " , TITULO as descricao"
                + " from OPERACOES_SISTEMAS OPSI)", "q"));
        aTitulo.setAutocompleteLength(2);
        
        aIntfTitulo = new DomText();
        aIntfTitulo.context(ctx).name("INTF_TITULO").label("Interface");
        aIntfTitulo.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT TITULO as valor"
                + " , TITULO as pesquisar"
                + " , TITULO as descricao"
                + " from INTERFACES_SISTEMAS INTF)", "q"));
        aIntfTitulo.setAutocompleteLength(1);
        
        aOperTitulo = new DomOpcao();
        aOperTitulo.context(ctx).name("OPER_TITULO").label("Operação").unbound(true).sqlExpression(true).group(true);
        
        
        qTitulo = new DomText();
        qTitulo.context(ctx).name("TITULO").label("Feature");
        qTitulo.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT TITULO as valor"
                + " , TITULO as pesquisar"
                + " , TITULO as descricao"
                + " from OPERACOES_SISTEMAS OPSI)", "q"));
        qTitulo.setAutocompleteLength(2);
        
        qOperacao = new DomUpper();
        qOperacao.context(ctx).name("OPERACAO").label("Operação");
        qOperacao.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT OPERACAO as valor"
                + " , OPERACAO as pesquisar"
                + " , OPERACAO as descricao"
                + " from OPERACOES_SISTEMAS OPSI)", "q"));
        qOperacao.setAutocompleteLength(2);
    }
    
    @Override
    public void initParameters() {
        pFuncaoId = new DomId(ctx, "FUNCAO_ID", "Função que está sendo mantida");
        pFuncaoId.setValue(ctx.getParameter("P_FUNCAO_ID"));
        if (pFuncaoId.isNotNull()) {
            aFuncaoId.setValue(pFuncaoId.getValue());
        }
    }
    
    @Override
    public List<Field> getParameters() {
        if (allParameters == null) {
            allParameters = new ArrayList<Field>();
            allParameters.add(pFuncaoId);
        }
        return allParameters;
    }
    
    @Override
    public void copyBindParameters() {
        EntOperacoesFuncoes record = getRecord();
        if (pFuncaoId.isNotNull()) {
            record.setFuncaoId(pFuncaoId.getValue());
        }
    }
    
    @Override
    public void retrieveBindParameters() {
        pFuncaoId.setValue(getRecord().valueOfFuncaoId());
    }
    
    @Override
    public String getQueryPkColumn() {
        return "OPFU.ID";
    }
    
    @Override
    public void setId(String value) {
        this.aId.setValue(value);
    }
    
    @Override
    public EntOperacoesFuncoes getRecord() {
        if (opfu == null) {
            opfu = new EntOperacoesFuncoes(ctx);
            rec = opfu;
        }
        return this.opfu;
    }
    
    @Override
    public String getContextName() {
        return aOperacao.toText();
    }
    
    @Override
    public String getDefaultOrderBy() {
        return "OPERACAO, INTF_TITULO, TITULO";
    }
    
    @Override
    public void translateInsertFks(Form form) {
        ModelQuery query;
        
        // OPERACAO_ID
        try {
            if (form.hasFields("OPERACAO")) {
                query = createQuery()
                        .select("OPSI.ID")
                        .from("OPERACOES_SISTEMAS", "OPSI")
                        .filter("OPSI.OPERACAO", form.get("OPERACAO"))
                        ;
                opfu.setOperacaoId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    @Override
    public void translateUpdateFks(Form form) {
        ModelQuery query;
        
        // FUNCAO_ID
        try {
            if (form.hasFields("FUNCAO_SISTEMA")) {
                query = createQuery()
                        .select("FUNC.ID")
                        .from("FUNCOES_SISTEMAS", "FUNC")
                        .filter("FUNC.FUNCAO_SISTEMA", form.get("FUNCAO_SISTEMA"))
                        ;
                opfu.setFuncaoId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
        
        // OPERACAO_ID
        try {
            if (form.hasFields("OPERACAO", "TITULO")) {
                query = createQuery()
                        .select("OPSI.ID")
                        .from("OPERACOES_SISTEMAS", "OPSI")
                        .filter("OPSI.OPERACAO", form.get("OPERACAO"))
                        .filter("OPSI.TITULO", form.get("TITULO"))
                        ;
                opfu.setOperacaoId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="QUERY">
    public class FQuery extends QueryFeature {
        
        @Override
        public void initForm() {
            addField(qOperacao, "opt");
            addField(qTitulo, "opt");
        }
        
        {
            setFeatureName("QUERY");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="LIST">
    public class FList extends ListFeature {
        
        @Override
        public void initForm() {
            addField(aOperacao, "view");
            addField(aIntfTitulo, "view");
            addField(aTitulo, "view");
        }
        
        {
            setFeatureName("LIST");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="INSERT">
    public class FInsert extends InsertFeature {
        
        @Override
        public void initForm() {
            aOperacao.setOptional(false);
            addField(aOperacao, "rqd");
            aIntfTitulo.setEditable(false);
            addField(aIntfTitulo, "view");
            aTitulo.setEditable(false);
            addField(aTitulo, "view");
        }
        
        @Override
        public void preDisplay() {
            try {
                popularOperacoes();
            } catch (AdsException ex) {
                throw new ApplicationRuntimeException(ex);
            }
        }
        
        {
            setFeatureName("INSERT");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="VIEW">
    public class FView extends ViewFeature {
        
        @Override
        public void initForm() {
            addField(aOperacao, "view");
            addField(aIntfTitulo, "view");
            addField(aTitulo, "view");
        }
        
        {
            setFeatureName("VIEW");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="DELETE">
    public class FDelete extends DeleteFeature {
        
        @Override
        public void initForm() {
            addField(aOperacao, "view");
            addField(aIntfTitulo, "view");
            addField(aTitulo, "view");
        }
        
        {
            setFeatureName("DELETE");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="IMPORT">
    public class FImport extends ImportFeature {
        
        @Override
        public void initForm() {
            aOperacao.setOptional(false);
            addField(aOperacao, "rqd");
            aFuncaoSistema.setOptional(false);
            addField(aFuncaoSistema, "rqd");
        }
        
        {
            setFeatureName("IMPORT");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="EXPORT">
    public class FExport extends ExportFeature {
        
        @Override
        public void initForm() {
            addField(aOperacao, "view");
            addField(aFuncaoSistema, "view");
        }
        
        {
            setFeatureName("EXPORT");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="OPERACAO_LOOKUP">
    public class FOperacaoLookup extends LookupFeature {
        
        @Override
        public void initForm() {
            aOperacao.setEditable(true);
            addField(aOperacao, "opt");
            aTitulo.setEditable(false);
            addField(aTitulo, "view");
            aIntfTitulo.setEditable(false);
            addField(aIntfTitulo, "view");
        }
        
        @Override
        public ModelQuery getLookupQuery() {
            return createQuery()
                    .select("OPSI.OPERACAO as OPERACAO")
                    .select("OPSI.TITULO as TITULO")
                    .select("INTF.TITULO as INTF_TITULO")
                    .from("OPERACOES_SISTEMAS", "OPSI")
                    .leftJoin("INTERFACES_SISTEMAS", "INTF", "INTF.ID = OPSI.INTERFACE_ID")
                    ;
        }
        
        {
            setFeatureName("OPERACAO_LOOKUP");
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="initFeatures">
    
    @Override
    protected void initFeatures() {
        addFeature(new FQuery());
        addFeature(new FList());
        addFeature(new FInsert());
        addFeature(new FView());
        addFeature(new FDelete());
        addFeature(new FImport());
        addFeature(new FExport());
        addFeature(new FOperacaoLookup());
        super.initFeatures();
    }
    
    @Override
    public String getDefaultFeature() {
        return "QUERY";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    
    @Override
    public Long getId() {
        return aId.getValue();
    }
    
    public void setId(Long value) {
        this.aId.setValue(value);
    }
    
    public Long getFuncaoId() {
        return aFuncaoId.getValue();
    }
    
    public void setFuncaoId(Long value) {
        this.aFuncaoId.setValue(value);
    }
    
    public String getOperacaoId() {
        return aOperacaoId.getValue();
    }
    
    public void setOperacaoId(String value) {
        this.aOperacaoId.setValue(value);
    }
    
    public String getFuncaoSistema() {
        return aFuncaoSistema.getValue();
    }
    
    public void setFuncaoSistema(String value) {
        this.aFuncaoSistema.setValue(value);
    }
    
    public String getOperacao() {
        return aOperacao.getValue();
    }
    
    public void setOperacao(String value) {
        this.aOperacao.setValue(value);
    }
    
    public String getTitulo() {
        return aTitulo.getValue();
    }
    
    public void setTitulo(String value) {
        this.aTitulo.setValue(value);
    }
    
    public String getIntfTitulo() {
        return aIntfTitulo.getValue();
    }
    
    public void setIntfTitulo(String value) {
        this.aIntfTitulo.setValue(value);
    }
    
    public String getOperTitulo() {
        return aOperTitulo.getValue();
    }
    
    public void setOperTitulo(String value) {
        this.aOperTitulo.setValue(value);
    }
    public String getQueryOperacao() {
        return qOperacao.getQueryValue();
    }
    
    public void setQueryOperacao(String value) {
        this.qOperacao.setQueryValue(value);
    }
    public String getQueryTitulo() {
        return qTitulo.getQueryValue();
    }
    
    public void setQueryTitulo(String value) {
        this.qTitulo.setQueryValue(value);
    }
    
    public Long getParameterFuncaoId() {
        return pFuncaoId.getValue();
    }
    
    public void setParameterFuncaoId(Long value) {
        pFuncaoId.setValue(value);
        aFuncaoId.setValue(value);
    }
    // </editor-fold>
    
    /**
     * Popular operações.
     * <p>
     * Popular as operações disponíveis.
     */
    private void popularOperacoes() throws AdsException {
        // <user-code id="806821" parentId="752578" hashcode="-34796ec5" length="120">
        aOperacaoId.addSqlOptions("select id, operacao||' - '||titulo" +
                " from operacoes_sistemas order by operacao");
        // </user-code>
    }
    
    @Override
    public String getJsServiceFile() {
        return "ui/rtsysinf/cad_funcao_operacao.js";
    }
}
